﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet5
    {
        private void Sheet5_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet5_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO で生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet5_Startup);
            this.Shutdown += new System.EventHandler(Sheet5_Shutdown);
        }

        #endregion

        private static int POS_GameSpecIdCol = 2;
        private static int POS_GameSpecIdRow = 4;
        private static int POS_GameSpecNameCol = 2;
        private static int POS_GameSpecNameRow = 1;
        private static int POS_CourseNameCol = 2;
        private static int POS_CourseNameRow = 3;
        private static int POS_MemoCol = 2;
        private static int POS_MemoRow = 2;

        private static int POS_BonusSpecNameCol = 1;
        private static int POS_BonusSpecSpecCol = 2;
        private static int POS_BonusSpecShortNameCol = 3;
        private static int POS_BonusSpecPointCol = 4;
        private static int POS_BonusSpecOrderCol = 5;
        private static int POS_BonusSpecIdCol = 6;
        private static int POS_BonusSpecStartRow = 7;

        public GameSpecInfo GetGameSpecInfo()
        {
            string id = "";
            string name = "";
            string memo = "";
            string courceName = "";

            if (((Excel.Range)Globals.Sheet5.Cells[POS_GameSpecIdRow, POS_GameSpecIdCol]).Value2 != null)
            {
                id = ((Excel.Range)Globals.Sheet5.Cells[POS_GameSpecIdRow, POS_GameSpecIdCol]).Value2.ToString();
            }
            if (((Excel.Range)Globals.Sheet5.Cells[POS_GameSpecNameRow, POS_GameSpecNameCol]).Value2 != null)
            {
                name = ((Excel.Range)Globals.Sheet5.Cells[POS_GameSpecNameRow, POS_GameSpecNameCol]).Value2.ToString();
            }
            if (((Excel.Range)Globals.Sheet5.Cells[POS_MemoRow, POS_MemoCol]).Value2 != null)
            {
                memo = ((Excel.Range)Globals.Sheet5.Cells[POS_MemoRow, POS_MemoCol]).Value2.ToString();
            }
            if (((Excel.Range)Globals.Sheet5.Cells[POS_CourseNameRow, POS_CourseNameCol]).Value2 != null)
            {
                courceName = ((Excel.Range)Globals.Sheet5.Cells[POS_CourseNameRow, POS_CourseNameCol]).Value2.ToString();
            }

            return new GameSpecInfo { Id = id, Name = name, Memo = memo, CourceName = courceName };
        }

        public void SetGameSpecInfo(GameSpecInfo info)
        {
            if (!String.IsNullOrEmpty(info.Id))
            {
                ((Excel.Range)Globals.Sheet5.Cells[POS_GameSpecIdRow, POS_GameSpecIdCol]).Value2 = info.Id;
            }
            if (!String.IsNullOrEmpty(info.Name))
            {
                ((Excel.Range)Globals.Sheet5.Cells[POS_GameSpecNameRow, POS_GameSpecNameCol]).Value2 = info.Name;
            }
            if (!String.IsNullOrEmpty(info.Memo))
            {
                ((Excel.Range)Globals.Sheet5.Cells[POS_MemoRow, POS_MemoCol]).Value2 = info.Memo;
            }
            if (!String.IsNullOrEmpty(info.CourceName))
            {
                ((Excel.Range)Globals.Sheet5.Cells[POS_CourseNameRow, POS_CourseNameCol]).Value2 = info.CourceName;
            }
        }

        public BonusSpecInfo[] GetBonusSpecInfo()
        {
            List<BonusSpecInfo> infos = new List<BonusSpecInfo>();
            int index = POS_BonusSpecStartRow;
            while (((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecNameCol]).Value2 != null)
            {
                string name = ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecNameCol]).Value2.ToString();
                string shortName = "";
                string spec = "";
                int point = 0;
                string id = "";
                int clearOrder = -1;

                if (((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecShortNameCol]).Value2 != null)
                {
                    shortName = ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecShortNameCol]).Value2.ToString();
                }
                if (((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecSpecCol]).Value2 != null)
                {
                    spec = ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecSpecCol]).Value2.ToString();
                }
                if (((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecPointCol]).Value2 != null)
                {
                    point = System.Convert.ToInt32(((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecPointCol]).Value2.ToString());
                }
                if (((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecIdCol]).Value2 != null)
                {
                    id = ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecIdCol]).Value2.ToString();
                }
                if (((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecOrderCol]).Value2 != null)
                {
                    string temp = ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecOrderCol]).Value2.ToString();
                    if (!Int32.TryParse(temp, out clearOrder))
                    {
                        clearOrder = -1;
                    }
                }
                if (String.IsNullOrEmpty(id))
                {
                    id = null;
                }

                infos.Add(new BonusSpecInfo { Id = id, ClearOrder = clearOrder, Name = name, ShorName = shortName, Specification = spec, Point = point });
                index++;
            }

            return infos.ToArray();
        }

        public void SetBonusSpecInfo(BonusSpecInfo[] infos)
        {
            int index = POS_BonusSpecStartRow;
            foreach (var info in infos)
            {
                if (info.Name!=null){
                    ((Excel.Range)Globals.Sheet5.Cells[index,POS_BonusSpecNameCol]).Value2=info.Name;
                }
                if (!String.IsNullOrEmpty(info.ShorName)){
                    ((Excel.Range)Globals.Sheet5.Cells[index,POS_BonusSpecShortNameCol]).Value2=info.ShorName;
                }
                if (!String.IsNullOrEmpty(info.Specification))
                {
                    ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecSpecCol]).Value2 = info.Specification;
                }
                if (info.Point > 0)
                {
                    ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecPointCol]).Value2 = info.Point;
                }
                if (info.ClearOrder >= 0)
                {
                    ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecOrderCol]).Value2 = info.ClearOrder;
                }
                if (!String.IsNullOrEmpty(info.Id))
                {
                    ((Excel.Range)Globals.Sheet5.Cells[index, POS_BonusSpecIdCol]).Value2 = info.Id;
                }
                index++;
            }
        }

    }

    public class GameSpecInfo
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Memo { get; set; }
        public string CourceName { get; set; }
    }

    public class BonusSpecInfo
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string ShorName { get; set; }
        public int Point { get; set; }
        public int ClearOrder { get; set; }
        public string Specification { get; set; }
    }
}
